package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.convert.Convert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.IDCardUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.CheckIn;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.DoTaskDto;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.dto.TaskCancelDto;
import com.zzyl.nursing.service.ICheckInService;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.NursingNameVo;
import com.zzyl.nursing.vo.NursingTaskDetailVo;
import com.zzyl.nursing.vo.NursingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



/**
 * 护理任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-13
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService {

    @Autowired
    private NursingTaskMapper nursingTaskMapper;

    @Autowired
    private ICheckInService checkInService;


    @Autowired
    private IElderService elderService;


    @Override
    public List<NursingTaskVo> selectNursingTaskList(NursingTaskDto nursingTaskDto) {

        List<NursingTaskVo> nursingTaskVos = nursingTaskMapper.selectNursingTaskList(nursingTaskDto);

        nursingTaskVos.forEach(nursingTaskVo -> {

            String nursingId = nursingTaskVo.getNursingId();

            if (StringUtils.isEmpty(nursingId)) {
                nursingTaskVo.setNursingName(null);
                return;
            }

            List<String> nursingName = new ArrayList<>();
            List<NursingNameVo> nursingNames;
            if (nursingId.contains(",")) {
                String[] split = nursingId.split(",");
                Long[] ids = Convert.convert(Long[].class, split);
                nursingNames = nursingTaskMapper.getUersByIds(ids);

            } else {
                long id = Long.parseLong(nursingId);
                nursingNames = nursingTaskMapper.getUersByIds(new Long[]{id});
            }
            nursingNames.forEach(nursingNameVo -> {
                nursingName.add(nursingNameVo.getNursingName());
            });

            nursingTaskVo.setNursingName(nursingName);
        });

        return nursingTaskVos;
    }

    @Override
    public NursingTaskDetailVo getNursingTaskDetail(Integer id) {

        NursingTaskDetailVo nursingTaskDetailVo =  nursingTaskMapper.getNursingTaskDetail(id);
        Long elderId = nursingTaskDetailVo.getElderId();
        CheckIn checkIn = checkInService.getOne(new LambdaQueryWrapper<CheckIn>().eq(CheckIn::getElderId, elderId));
        nursingTaskDetailVo.setNursingLevelName(checkIn.getNursingLevelName());
        Elder elder = elderService.getOne(new LambdaQueryWrapper<Elder>().eq(Elder::getId, elderId));


        int age = IDCardUtils.getAgeByIdCard(elder.getIdCardNo());
        nursingTaskDetailVo.setAge(age);
        int genderFromIdCard = IDCardUtils.getGenderFromIdCard(elder.getIdCardNo());

        if (genderFromIdCard == 0) {
            nursingTaskDetailVo.setSex("男");
        } else {
            nursingTaskDetailVo.setSex("女");
        }

        String updateBy = nursingTaskDetailVo.getUpdateBy();
        if (updateBy != null){
            long updateId = Long.parseLong(updateBy);
            List<NursingNameVo> uersByIds = nursingTaskMapper.getUersByIds(new Long[]{updateId});
            NursingNameVo nameVo = uersByIds.get(0);
            nursingTaskDetailVo.setUpdater(nameVo.getNursingName());
        }else {
            nursingTaskDetailVo.setUpdater("无");
        }


        String nursingId = nursingTaskDetailVo.getNursingId();

        if (StringUtils.isEmpty(nursingId)) {
            nursingTaskDetailVo.setNursingName(null);
            return nursingTaskDetailVo;
        }

        List<String> nursingName = new ArrayList<>();

        List<NursingNameVo> nursingNames;
        if (nursingId.contains(",")) {
            String[] split = nursingId.split(",");
            Long[] ids = Convert.convert(Long[].class, split);
            nursingNames = nursingTaskMapper.getUersByIds(ids);

        } else {
            long uid = Long.parseLong(nursingId);
            nursingNames = nursingTaskMapper.getUersByIds(new Long[]{uid});
        }
        nursingNames.forEach(nursingNameVo -> {
            nursingName.add(nursingNameVo.getNursingName());
        });
        nursingTaskDetailVo.setNursingName(nursingName);

        return nursingTaskDetailVo;
    }

    @Override
    public void cancelTask(TaskCancelDto taskCancelDto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(Long.valueOf(taskCancelDto.getTaskId()));
        nursingTask.setStatus(3);
        nursingTask.setCancelReason(taskCancelDto.getReason());
        this.updateById(nursingTask);

    }

    @Override
    public void doTask(DoTaskDto doTaskDto) {
        NursingTask  nursingTask = new NursingTask();
        nursingTask.setStatus(2);
        nursingTask.setEstimatedServerTime(doTaskDto.getEstimatedServerTime());
        nursingTask.setMark(doTaskDto.getMark());
        nursingTask.setId(Long.valueOf(doTaskDto.getTaskId()));
        nursingTask.setTaskImage(doTaskDto.getTaskImage());
        nursingTask.setRealServerTime(LocalDateTime.now());
        this.updateById(nursingTask);
    }

    @Override
    public void updateTaskTime(DoTaskDto doTaskDto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(Long.valueOf(doTaskDto.getTaskId()));
        nursingTask.setEstimatedServerTime(doTaskDto.getEstimatedServerTime());
        this.updateById(nursingTask);
    }


}
